package dicky.fortunetelling;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import dicky.fortunetelling.logic.CommonUtil;
import dicky.fortunetelling.logic.DataArray;


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class namefortuneForTab extends Activity {
	private Button btn1,btn2;
	private EditText lastname1,lastname2,name1,name2;
	private TextView t_lastname1,t_lastname2,t_name1,t_name2,title1,title2,title3,c,five,text1;
	private ListView listView;
	private int type,c1,c2,c3,c4,c5,l1,l2,n1,n2;
	private List list=null;
	private ViewAdapter dataAdapter;
	
	private String tag=namefortuneForTab.class.getSimpleName();
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bud = getIntent().getExtras();

		if (bud != null && bud.containsKey("type")) {
			type = bud.getInt("type");
		}
        
        Log.d(tag, "onCreate type="+type);
        
        switch (type) {
		case 1:
			setContentView(R.layout.namefortune1);
			lastname1=(EditText)findViewById(R.id.lastname1);
	        name1=(EditText)findViewById(R.id.name1);
			break;
		case 2:
			setContentView(R.layout.namefortune2);
			lastname1=(EditText)findViewById(R.id.lastname1);
	        name1=(EditText)findViewById(R.id.name1);
	        name2=(EditText)findViewById(R.id.name2);
			break;
		case 3:
			setContentView(R.layout.namefortune3);
			lastname1=(EditText)findViewById(R.id.lastname1);
	        lastname2=(EditText)findViewById(R.id.lastname2);
	        name1=(EditText)findViewById(R.id.name1);
			break;
		case 4:
			setContentView(R.layout.namefortune4);
			lastname1=(EditText)findViewById(R.id.lastname1);
	        lastname2=(EditText)findViewById(R.id.lastname2);
	        name1=(EditText)findViewById(R.id.name1);
	        name2=(EditText)findViewById(R.id.name2);
			break;
		}
        
        btn1=(Button)findViewById(R.id.button1);
        btn1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				namefortune();
			}
		});

        btn2=(Button)findViewById(R.id.button2);
        btn2.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				CommonUtil.GetGoodNumber(namefortuneForTab.this);
			}
		});
        
		listView = (ListView) findViewById(R.id.listview);
        
    }
	
	private void namefortune(){
		try{
			switch (type) {
			case 1:
				l1=Integer.parseInt(lastname1.getText().toString());
				n1=Integer.parseInt(name1.getText().toString());
				c1=l1+1;
				c2=l1+n1;
				c3=n1+1;
				c4=2;
				c5=l1+n1;
				break;
			case 2:
				l1=Integer.parseInt(lastname1.getText().toString());
				n1=Integer.parseInt(name1.getText().toString());
				n2=Integer.parseInt(name2.getText().toString());
				c1=l1+1;
				c2=l1+n1;
				c3=n1+n2;
				c4=n1+1;
				c5=l1+n1+n2;
				break;
			case 3:
				l1=Integer.parseInt(lastname1.getText().toString());
				l2=Integer.parseInt(lastname2.getText().toString());
				n1=Integer.parseInt(name1.getText().toString());
				c1=l1+l2;
				c2=l2+n1;
				c3=n1+1;
				c4=l1+1;
				c5=l1+l2+n1;
				break;
			case 4:
				l1=Integer.parseInt(lastname1.getText().toString());
				l2=Integer.parseInt(lastname2.getText().toString());
				n1=Integer.parseInt(name1.getText().toString());
				n2=Integer.parseInt(name2.getText().toString());
				c1=l1+l2;
				c2=l2+n1;
				c3=n1+n2;
				c4=l1+n2;
				c5=l1+l2+n1+n2;
				break;
			}
			HashMap map=null;
			list=new ArrayList();
			String c6="";
			String five="";
			String text="";
			
			c6+=(c1+"").substring((c1+"").length()-1, (c1+"").length());
			c6+=(c2+"").substring((c2+"").length()-1, (c2+"").length());
			c6+=(c3+"").substring((c3+"").length()-1, (c3+"").length());
			
			five=DataArray.GetYinYang(c1)+DataArray.GetFive(c1);
			text=DataArray.GetNumber1(c1)+"\n\n"+DataArray.GetNumber2(c1);
			map=new HashMap ();
			map.put("title", "天格");
			map.put("c",c1);
			map.put("five", five);
			map.put("text", text);
			list.add(map);

			five=DataArray.GetYinYang(c2)+DataArray.GetFive(c2);
			text=DataArray.GetNumber1(c2)+"\n\n"+DataArray.GetNumber2(c2);
			map=new HashMap ();
			map.put("title", "人格");
			map.put("c",c2);
			map.put("five", five);
			map.put("text", text);
			list.add(map);
			
			five=DataArray.GetYinYang(c3)+DataArray.GetFive(c3);
			text=DataArray.GetNumber1(c3)+"\n\n"+DataArray.GetNumber2(c3);
			map=new HashMap ();
			map.put("title", "地格");
			map.put("c",c3);
			map.put("five", five);
			map.put("text", text);
			list.add(map);

			five=DataArray.GetYinYang(c4)+DataArray.GetFive(c4);
			text=DataArray.GetNumber1(c4)+"\n\n"+DataArray.GetNumber2(c4);
			map=new HashMap ();
			map.put("title", "外格");
			map.put("c",c4);
			map.put("five", five);
			map.put("text", text);
			list.add(map);

			five=DataArray.GetYinYang(c5)+DataArray.GetFive(c5);
			text=DataArray.GetNumber1(c5)+"\n\n"+DataArray.GetNumber2(c5);
			map=new HashMap ();
			map.put("title", "总格");
			map.put("c",c5);
			map.put("five", five);
			map.put("text", text);
			list.add(map);

			five=DataArray.GetFive(c1)+DataArray.GetFive(c2)+DataArray.GetFive(c3);
			text=DataArray.GetSanCai(five);
			map=new HashMap ();
			map.put("title", "三才");
			map.put("c",c6);
			map.put("five", five);
			map.put("text", text);
			list.add(map);
			
			dataAdapter = new ViewAdapter(namefortuneForTab.this, R.layout.list,list);
			listView.setAdapter(dataAdapter);
		} catch (Exception e) {
			Log.e(tag, e.toString());
			e.printStackTrace();
			Toast.makeText(namefortuneForTab.this, "录入数据有误", Toast.LENGTH_LONG).show();
		}
	}
	
	private class ViewAdapter extends BaseAdapter {
		private Context mContext;
		private List list;
		private int textViewResourceId;

		public ViewAdapter(Context c,int textViewResourceId, List list) {
			this.mContext = c;
			this.textViewResourceId = textViewResourceId;
			this.list = list;
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public HashMap getItem(int position) {
			return (HashMap)list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				LayoutInflater vi = (LayoutInflater) namefortuneForTab.this
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = vi.inflate(textViewResourceId, null);
			}
			HashMap map=getItem(position);
			
			title1=(TextView) convertView.findViewById(R.id.title1);
			title1.getPaint().setFakeBoldText(true);
			title2=(TextView) convertView.findViewById(R.id.title2);
			title2.getPaint().setFakeBoldText(true);
			title3=(TextView) convertView.findViewById(R.id.title3);
			title3.getPaint().setFakeBoldText(true);
			
			c=(TextView) convertView.findViewById(R.id.c);
			five=(TextView) convertView.findViewById(R.id.five);
			text1=(TextView) convertView.findViewById(R.id.text1);
			
			title1.setText(map.get("title")+"");
			c.setText(map.get("c")+"");
			five.setText(map.get("five")+"");
			int color=CommonUtil.GetFiveColor(map.get("five")+"");
			five.setTextColor(getResources().getColor(color));
			text1.setText(map.get("text")+"");
			return convertView;
		}
	}
}
